<?php

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use \interop\Container\ContainerInterface;
require '/lib/xml.php';  

class PublishController 
{
    protected $app;

    public function __construct(ContainerInterface $ci)
    {
        $this->app = $ci;
    }

    public function readXml(Request $request, Response $response, $args){
 
    	$xml = new ome_xml();
		$arr = $xml->xml2array(dirname(__dir__).'/../vendor/taobao/item.xml');	
		$arr = $arr['itemRule']['field'];
		// print_r($arr);exit;
		$rs = array();
		$arr1 = array('name'=>'requiredRule','value'=>'');
		foreach($arr as $k=>$v){
			if($v['type'] == 'input'){
				$rs[$k]['name'] = $v['name'];
				$rs[$k]['type'] = $v['type'];
				if(@in_array('require', $v['rules']['rule'])){
					$rs[$k]['rule'] = 'require';
				}
				if(@in_array($arr1, $v['rules']['rule'])){
					$rs[$k]['rule'] = 'require';
				}
			}
			if(@$v['options']){
			if($v['options']['option']){
				if(count($v['options']['option']) == count($v['options']['option'],1)){
					$rs[$k]['option'][] = $v['options']['option']['displayName'];
					$rs[$k]['name'] = $v['name'];
					$rs[$k]['type'] = $v['type'];
					if(@in_array('require', $v['rules']['rule'])){
						$rs[$k]['rule'] = 'require';
					}
					if(@in_array($arr1, $v['rules']['rule'])){
						$rs[$k]['rule'] = 'require';
					}
					// print_r($rs);exit;
				}else{
					// print_r($v['options']['option']);exit;
					foreach(@$v['options']['option'] as $c){
						// print_r($c);
						if(@isset($c['displayName'])){
							// print_r($c['displayName']);
							$rs[$k]['option'][] = $c['displayName'];
							$rs[$k]['name'] = $v['name'];
							$rs[$k]['type'] = $v['type'];
							if(@in_array('require', $v['rules']['rule'])){
								$rs[$k]['rule'] = 'require';
							}
							if(@in_array($arr1, $v['rules']['rule'])){
								$rs[$k]['rule'] = 'require';
							}
						}
					}
				}
			}
			}
   		}
   		return $response->withJson($rs);
	}
}